<?php
// +----------------------------------------------------------------------
// | title: 标题注释
// +----------------------------------------------------------------------
// | Author: TZ.Leong
// +----------------------------------------------------------------------
// | Date: 2019/7/30 11:59
// +----------------------------------------------------------------------

namespace app\single\api;


use app\single\model\MailModel;
use think\facade\Log;

class Mail
{
    public function run($params)
    {
        $ret["ret"] = 1;
        $data = json_decode(base64_decode($params["data"]), true);
        //邮件内容为空
        if (empty($data["content"])) {
            $ret["data"]["syn_status"] = 5;
            $ret["data"]["syn_desc"] = "Content is null";
            return $ret;
        }
        $mailModel = new MailModel();
        //检测是否存在
        $checkId = $mailModel->checkInfo(["title" => $data["title"], "send_time" => $data["send_time"], "condition" => $data["condition"]]);
        if (!empty($checkId)) {
            //邮件已经存在了
            $ret["data"]["syn_status"] = 5;
            $ret["data"]["syn_desc"] = "mail have exists";
            return $ret;
        }
        try {
            $data["create_time"] = time();
            $addData = [
                "title" => $data["title"],
                "content" => $data["content"],
                "attachment" => $data["attachment"],
                "condition" => $data["condition"],
                "send_time" => $data["send_time"],
                "admin" => $data["admin"],
                "create_time" => time(),
                "mail_type" => $data["mail_type"] ?? 1,
            ];
            //检测完成，加入数据库
            if ($mailModel->insert($addData)) {
                $ret["data"]["syn_status"] = 4;
                $ret["data"]["syn_desc"] = "send success!";
            } else {
                $ret["data"]["syn_status"] = 5;
                $ret["data"]["syn_desc"] = "send fail!";
            }
        } catch (\Exception $e) {
            $ret["data"]["syn_status"] = 5;
            $ret["data"]["syn_desc"] = $e->getMessage();
        }
        return $ret;
    }
}